<!-- 车辆详情 -->
<template>
  <div
    class="dashboard-container vehicle-detail"
  >
    <div class="app-container">
      <el-card
        shadow="never"
        style="height: 1080px;"
      >
        <el-tabs
          v-model="activeName"
        >
          <el-tab-pane
            label="基本信息"
            name="first"
          >
            <el-form
              ref="vehicleDetailForm"
              :model="vehicleDetailForm"
              :rules="vehicleDetailRules"
              label-width="80px"
              label-position="left"
            >
              <el-col :span="8">
                <el-form-item
                  label="车辆编号"
                  prop="id"
                >
                  <span v-show="vehicleState === 0">{{ vehicleDetailForm.id }}</span>
                  <el-input
                    v-show="vehicleState === 1"
                    v-model="vehicleDetailForm.id"
                    disabled
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="车牌号码"
                  prop="licensePlate"
                >
                  <span v-show="vehicleState === 0">{{ vehicleDetailForm.licensePlate }}</span>
                  <el-input
                    v-show="vehicleState === 1"
                    v-model="vehicleDetailForm.licensePlate"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="车型名称"
                  prop="truckTypeName"
                >
                  <span v-show="vehicleState === 0">{{ vehicleDetailForm.truckTypeName }}</span>
                  <el-select
                    v-show="vehicleState === 1"
                    v-model="vehicleDetailForm.truckTypeId"
                  >
                    <el-option
                      v-for="item in carTypeOptions"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="车辆体积"
                  prop="allowableVolume"
                >
                  <span v-show="vehicleState === 0">{{ vehicleDetailForm.allowableVolume }}</span>
                  <el-input
                    v-show="vehicleState === 1"
                    v-model="vehicleDetailForm.allowableVolume"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="车辆载重"
                  prop="allowableLoad"
                >
                  <span v-show="vehicleState === 0">{{ vehicleDetailForm.allowableLoad }}</span>
                  <el-input
                    v-show="vehicleState === 1"
                    v-model="vehicleDetailForm.allowableLoad"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="GPSID"
                  prop="deviceGpsId"
                >
                  <span v-show="vehicleState === 0">{{ vehicleDetailForm.deviceGpsId }}</span>
                  <el-input
                    v-show="vehicleState === 1"
                    v-model="vehicleDetailForm.deviceGpsId"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="图片信息">
                  <el-upload
                    v-show="vehicleState === 1"
                    class="upload-demo"
                    list-type="picture-card"
                    action="#"
                    :http-request="upload"
                    :before-upload="beforeUpload"
                  >
                    <i class="el-icon-plus"></i>
                    <div
                      slot="tip"
                      class="el-upload__tip"
                    >注：图片大小不超过5M；仅能上传 PNG JPEG JPG类型图片；建议上传400*300尺寸的图片；最多上传2张</div>
                  </el-upload>
                  <img
                    v-if="vehicleState === 0"
                    :src="vehicleDetailForm.picture"
                    alt=""
                    class="businessLicenseUrl"
                  >
                </el-form-item>
              </el-col>
            </el-form>
            <div
              class="btnBox"
              style="text-align: center;"
            >
              <el-button
                v-show="vehicleState === 0"
                type="warning"
                @click="vehicleState = 1"
              >编辑</el-button>
              <el-button
                v-show="vehicleState === 1"
                type="warning"
                @click="vehicleState = 0"
              >取消</el-button>
              <el-button
                v-show="vehicleState === 1"
                type="warning"
                @click="updateTruckType"
              >保存</el-button>
            </div>
          </el-tab-pane>
          <el-tab-pane
            label="行驶证信息"
            name="second"
          >
            <el-form
              ref="transportCertificateForm"
              :model="transportCertificateForm"
              :rules="transportCertificateRules"
              label-width="100px"
              label-position="left"
            >
              <el-col :span="8">
                <el-form-item
                  label="行驶证号码"
                  prop="transportCertificateNumber"
                >
                  <span v-show="transportCertificateState === 0">{{ transportCertificateForm.transportCertificateNumber }}</span>
                  <el-input
                    v-show="transportCertificateState === 1"
                    v-model="transportCertificateForm.transportCertificateNumber"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="发动机号码"
                  prop="engineNumber"
                >
                  <span v-show="transportCertificateState === 0">{{ transportCertificateForm.engineNumber }}</span>
                  <el-input
                    v-show="transportCertificateState === 1"
                    v-model="transportCertificateForm.engineNumber"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="注册时间"
                  prop="registrationDate"
                >
                  <span v-show="transportCertificateState === 0">{{ transportCertificateForm.registrationDate }}</span>
                  <el-date-picker
                    v-show="transportCertificateState === 1"
                    v-model="transportCertificateForm.registrationDate"
                    type="date"
                    placeholder="选择日期"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="报废日期"
                  prop="mandatoryScrap"
                >
                  <span v-show="transportCertificateState === 0">{{ transportCertificateForm.mandatoryScrap }}</span>
                  <el-date-picker
                    v-show="transportCertificateState === 1"
                    v-model="transportCertificateForm.mandatoryScrap"
                    type="date"
                    placeholder="选择日期"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="整备质量"
                  prop="overallQuality"
                >
                  <span v-show="transportCertificateState === 0">{{ transportCertificateForm.overallQuality }}</span>
                  <el-input
                    v-show="transportCertificateState === 1"
                    v-model="transportCertificateForm.overallQuality"
                  >
                    <template slot="suffix">吨</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="检验有效期"
                  prop="expirationDate"
                >
                  <span v-show="transportCertificateState === 0">{{ transportCertificateForm.expirationDate }}</span>
                  <el-date-picker
                    v-show="transportCertificateState === 1"
                    v-model="transportCertificateForm.expirationDate"
                    type="date"
                    placeholder="选择日期"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="核定载质量"
                  prop="allowableWeight"
                >
                  <span v-show="transportCertificateState === 0">{{ transportCertificateForm.allowableWeight }}</span>
                  <el-input
                    v-show="transportCertificateState === 1"
                    v-model="transportCertificateForm.allowableWeight"
                  >
                    <template slot="suffix">吨</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="16">
                <el-form-item
                  label="有效期"
                  prop="validityPeriod"
                >
                  <span v-show="transportCertificateState === 0">{{ transportCertificateForm.validityPeriod }}</span>
                  <el-date-picker
                    v-show="transportCertificateState === 1"
                    v-model="transportCertificateForm.validityPeriod"
                    type="date"
                    placeholder="选择日期"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="图片信息">
                  <el-upload
                    v-show="transportCertificateState === 1"
                    class="upload-demo"
                    list-type="picture-card"
                    action="#"
                    :http-request="uploadTransportCertificate"
                    :before-upload="beforeUpload"
                  >
                    <i class="el-icon-plus"></i>
                    <div
                      slot="tip"
                      class="el-upload__tip"
                    >注：图片大小不超过5M；仅能上传 PNG JPEG JPG类型图片；建议上传400*300尺寸的图片；最多上传2张</div>
                  </el-upload>
                  <img
                    v-if="transportCertificateState === 0"
                    :src="transportCertificateForm.picture"
                    alt=""
                    class="businessLicenseUrl"
                  >
                </el-form-item>
              </el-col>
            </el-form>
            <div
              class="btnBox"
              style="text-align: center;"
            >
              <el-button
                v-show="transportCertificateState === 0"
                type="warning"
                @click="transportCertificateState = 1"
              >编辑</el-button>
              <el-button
                v-show="transportCertificateState === 1"
                type="warning"
                @click="transportCertificateState = 0"
              >取消</el-button>
              <el-button
                v-show="transportCertificateState === 1"
                type="warning"
                @click="updateTransportCertificate"
              >保存</el-button>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>
<script>
import { truckTypeDetail, getCarTypeList, imgUpload, truckTypeUpdate, truckTypeDrivingLicenseDetail, truckTypeDrivingLicense } from '@/api/transit'
export default {
  data () {
    return {
      activeName: 'first',
      vehicleDetailForm: {},
      vehicleState: 0, // 基本信息状态--0：查看；--1：编辑
      transportCertificateState: 0,
      carTypeOptions: [],
      vehicleDetailRules: {
        id: [
          { required: true, message: '请输入车辆编号', trigger: 'blur' }
        ],
        licensePlate: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' }
        ],
        truckTypeName: [
          { required: true, message: '请选择车辆类型', trigger: 'blur' }
        ],
        allowableVolume: [
          { required: true, message: '请输入实载体积', trigger: 'blur' }
        ],
        allowableLoad: [
          { required: true, message: '请输入实载重量', trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: '请输入GPS设备ID', trigger: 'blur' }
        ]
      },
      transportCertificateForm: {},
      transportCertificateRules: {
        allowableWeight: [
          { required: true, message: '请输入核定载质量', trigger: 'blur' }
        ],
        engineNumber: [
          { required: true, message: '请输入发动机编号', trigger: 'blur' }
        ],
        expirationDate: [
          { required: true, message: '请选择检验有效期', trigger: 'blur' }
        ],
        mandatoryScrap: [
          { required: true, message: '请选择国家强制报废日期', trigger: 'blur' }
        ],
        overallQuality: [
          { required: true, message: '请输入整备质量', trigger: 'blur' }
        ],
        registrationDate: [
          { required: true, message: '请选择注册时间', trigger: 'blur' }
        ],
        transportCertificateNumber: [
          { required: true, message: '请输入行驶证号', trigger: 'blur' }
        ],
        validityPeriod: [
          { required: true, message: '请选择行驶证有效期', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.getTruckTypeDetail()
    this.getCarTypeOptions()
    this.getTransportCertificate()
  },
  methods: {
    async getTruckTypeDetail() {
      const res = await truckTypeDetail(this.$route.query.id)
      this.vehicleDetailForm = res.data
    },
    async getCarTypeOptions() {
      const res = await getCarTypeList()
      this.carTypeOptions = res.data
    },
    beforeUpload(file) {
      const isPNGorJPG = file.type === ('image/png' || 'image/jpg' || 'image/jpeg')
      const isLt5M = file.size / 1024 / 1024 < 5

      if (!isPNGorJPG) {
        this.$message.error('上传的文件只能是 PNG、JPG、JPEG 格式!')
      }
      if (!isLt5M) {
        this.$message.error('上传的文件大小不能超过 5MB!')
      }
      return isPNGorJPG && isLt5M
    },
    async upload(res) {
      // file：要上传给后端接口的文件对象
      const { file } = res
      // 上传文件
      // 1.按照接口要求准备一个符合类型的formData对象 然后把参数加入进去
      const fd = new FormData() // FormData实例对象，内部是空的
      fd.append('file', file)
      // 2.调用接口
      const _res = await imgUpload(fd)
      this.vehicleDetailForm.picture = _res.data
    },
    updateTruckType() {
      this.$refs.vehicleDetailForm.validate(async valid => {
        if (valid) {
          const res = await truckTypeUpdate(this.$route.query.id, this.vehicleDetailForm)
          if (res.code === 200) {
            this.$message.success('车辆更新成功')
            this.vehicleState = 0
            this.getTruckTypeDetail()
          } else {
            this.$message.warning(res.msg)
          }
        }
      })
    },
    async getTransportCertificate() {
      const res = await truckTypeDrivingLicenseDetail(this.$route.query.id)
      this.transportCertificateForm = res.data
    },
    async uploadTransportCertificate(res) {
      // file：要上传给后端接口的文件对象
      const { file } = res
      // 上传文件
      // 1.按照接口要求准备一个符合类型的formData对象 然后把参数加入进去
      const fd = new FormData() // FormData实例对象，内部是空的
      fd.append('file', file)
      // 2.调用接口
      const _res = await imgUpload(fd)
      this.transportCertificateForm.picture = _res.data
    },
    updateTransportCertificate() {
      this.$refs.transportCertificateForm.validate(async valid => {
        if (valid) {
          console.log(valid)
          const res = await truckTypeDrivingLicense(this.$route.query.id, this.transportCertificateForm)
          if (res.code === 200) {
            this.$message.success('行驶证信息更新成功')
            this.transportCertificateState = 0
            this.getTransportCertificate()
          } else {
            this.$message.warning(res.msg)
          }
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.active {
  color: #ff643d;
}
</style>

<style lang="scss" scoped>
.vehicle-detail {
  ::v-deep .el-container{
    flex-direction: column;
    margin-left: 0px;

  }
  .in{
    height: calc(100vh - 205px);
    // height: 100%;
  }
  .aside-box {
    background: #ffffff;
    border-radius: 4px;
    // width: 150px;
    padding: 24px 37px 24px 37px;
    box-sizing: border-box;

    // min-height: calc(100vh - 50px);
    ::v-deep .el-aside {
      padding-bottom: 14px;
      width: 100%!important;
      display: flex;
      background-color: #ffffff;
      border-bottom:1px solid #E5E7EC ;
      text-align: left;
      font-size: 14px;
      .aside-item {
        // margin-top: 38px;
        cursor: pointer;
        &:first-child {
          margin-right: 53px;
          padding-left: 25px;

        }
      }
      .aside-item.active{
        font-size: 16px;
        font-weight: bold;
        color: #20232A;
        &:after{
          content: '';
          display: block;
          background-color:#E15536 ;
          height: 3px;
          width: 49px;
          margin: 0 auto;
          position: relative;
          top: 14px;
        }
      }
    }
  }

  ::v-deep .el-tabs__item {
    font-family: PingFangSC-Regular;
    font-size: 16px;
    color: #4E5969;
    line-height: 24px;
    font-weight: 400;
  }

  ::v-deep .is-active {
    font-family: PingFangSC-Semibold;
    font-size: 16px;
    color: #20232A;
    line-height: 24px;
    font-weight: 600;
  }

  ::v-deep .el-tabs__active-bar {
    background: #E15536;
    border-radius: 4px 4px 0px 0px;
  }
}
</style>
